<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: admin_escalations.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/tickets/functions_escalations.php");

require_once ("./includes/functions_html.php");
$template->loadLanguageSection("tickets");


/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing escalation rules data
*/
function _escalationRuleFields($arg)
{
	global $_SWIFT, $_sladays;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["slaplans.title"] = $record["plantitle"];
	$record["escalationrules.title"] = '<a href="index.php?_m=tickets&_a=editescalationrule&escalationruleid='.intval($record["escalationruleid"]).'">'.$record["title"].'</a>';

	$record["options"] = '<a href="index.php?_m=tickets&_a=editescalationrule&escalationruleid='. $record["escalationruleid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=tickets&_a=manageescalationrules&do=delete&escalationruleid='. $record["escalationruleid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Mass Action: Delete Escalation Rules
*/
function _maDeleteEscalationRules()
{
	global $dbCore, $_SWIFT;

	if ($_SWIFT["isdemo"] == true)
	{
		return false;
	}

	if (is_array($_POST["itemid"]))
	{
		deleteEscalationRules($_POST["itemid"]);
	}
}




/**
* ###############################################
* MANAGE ESCALATION RULES
* ###############################################
*/
if ($eventaction == "manageescalationrules")
{
	if ($_GET["do"] == "delete" && !empty($_GET["escalationruleid"]) && $_SWIFT["isdemo"] != true)
	{
		$infomessage = sprintf($_SWIFT["language"]["ruledeleteconfirm"], $_SWIFT["escalationrulecache"][$_GET["escalationruleid"]]["title"]);

		deleteEscalationRules(array($_GET["escalationruleid"]));
	}

	if (!empty($_REQUEST["insertrule"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["ruleinsertconfirm"], $_REQUEST["insertrule"]);
	} else if (!empty($_REQUEST["updaterule"])) {
		$infomessage = sprintf($_SWIFT["language"]["ruleupdateconfirm"], $_REQUEST["updaterule"]);
	}

	$options["recordsperpage"] = "6";
	$options["sortby"] = "escalationrules.title";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteEscalationRules";

	$options["idname"] = "escalationruleid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["ruletitle"];
	$options["advancedsearch"][0]["query"] = 'SELECT escalationrules.*, slaplans.title AS plantitle FROM `'. TABLE_PREFIX .'escalationrules` AS escalationrules LEFT JOIN `'. TABLE_PREFIX .'slaplans` AS slaplans ON (escalationrules.slaplanid = slaplans.slaplanid) WHERE escalationrules.title LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_escalationRuleFields";

	$fields[0]["name"] = "escalationrules.title";
	$fields[0]["title"] = $_SWIFT["language"]["ruletitle"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "slaplans.title";
	$fields[1]["title"] = $_SWIFT["language"]["plantitle"];
	$fields[1]["width"] = "250";
	$fields[1]["align"] = "center";

	$fields[2]["type"] = "custom";
	$fields[2]["name"] = "options";
	$fields[2]["title"] = $_SWIFT["language"]["options"];
	$fields[2]["width"] = "150";
	$fields[2]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["escalationrules"]." > ".$_SWIFT["language"]["manageescalationrules"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageescalationrules" title="'.$_SWIFT["language"]["escalationrules"].'">'.$_SWIFT["language"]["escalationrules"].'</a>', $_SWIFT["language"]["desc_escalationrules"] , 7);
	printInfoBox($infomessage);

	$grid->start("escalationrules", $_SWIFT["language"]["escalationrulelist"], 'SELECT escalationrules.*, slaplans.title AS plantitle FROM `'. TABLE_PREFIX .'escalationrules` AS escalationrules LEFT JOIN `'. TABLE_PREFIX .'slaplans` AS slaplans ON (escalationrules.slaplanid = slaplans.slaplanid) $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'escalationrules`;', 'SELECT escalationrules.*, slaplans.title AS plantitle FROM `'. TABLE_PREFIX .'escalationrules` AS escalationrules LEFT JOIN `'. TABLE_PREFIX .'slaplans` AS slaplans ON (escalationrules.slaplanid = slaplans.slaplanid) WHERE escalationrules.title LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	$grid->display("escalationrules");
	
	$interface->adminFooter();





/**
* ###############################################
* INSERT ESCALATION RULE
* ###############################################
*/
} else if ($eventaction == "insertescalationrule") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["slaplanid"]) == "" || trim($_POST["departmentid"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			$escalationruleid = insertEscalationRule($_POST["title"], $_POST["slaplanid"], $_POST["staffid"], $_POST["priorityid"], $_POST["ticketstatusid"], $_POST["departmentid"]);

			printRedirect(sprintf($_SWIFT["language"]["ruleinsertconfirm"], $_POST["title"]), "index.php?_m=tickets&_a=manageescalationrules&insertrule=".urlencode($_POST["title"]));
			exit;
		}
	}

	// ======= Start Interface Output =======
	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["escalationrules"]." > ".$_SWIFT["language"]["insertescalationrule"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageescalationrules" title="'.$_SWIFT["language"]["escalationrules"].'">'.$_SWIFT["language"]["escalationrules"].'</a> &raquo; <a href="index.php?_m=tickets&_a=insertescalationrule" title="'.$_SWIFT["language"]["insert"].'">'.$_SWIFT["language"]["insert"].'</a>', $_SWIFT["language"]["desc_escalationrules"] , 7);
	
	// Check for sla plans
	if (!count($_SWIFT["slaplancache"]))
	{
		$errormessage = iif($errormessage=="", $_SWIFT["language"]["errorinsertrule"], $errormessage);
	}

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderEscalationForm(INSERT);

	$template->assign("backurl", "index.php?_m=tickets&_a=manageescalationrules");

	$interface->adminFooter();







/**
* ###############################################
* EDIT ESCALATION RULE
* ###############################################
*/
} else if ($eventaction == "editescalationrule") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["slaplanid"]) == "" || trim($_POST["departmentid"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["language"]["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			updateEscalationRule($_POST["escalationruleid"], $_POST["title"], $_POST["slaplanid"], $_POST["staffid"], $_POST["priorityid"], $_POST["ticketstatusid"], $_POST["departmentid"]);

			printRedirect(sprintf($_SWIFT["language"]["ruleupdateconfirm"], $_POST["title"]), "index.php?_m=tickets&_a=manageescalationrules&updaterule=".urlencode($_POST["title"]));
			exit;
		}
	}

	// ======= Fetch All The Data =======
	$_escalationrule = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."escalationrules` WHERE `escalationruleid` = '". intval($_REQUEST["escalationruleid"]) ."';");
	if (empty($_escalationrule["escalationruleid"]))
	{
		trigger_error($_SWIFT["language"]["invalidescalationrule"], E_USER_ERROR);
	}

	$_POST = array_merge($_POST, $_escalationrule);

	// ======= Start Interface Output =======
	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["escalationrules"]." > ".$_SWIFT["language"]["editescalationrule"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageescalationrules" title="'.$_SWIFT["language"]["escalationrules"].'">'.$_SWIFT["language"]["escalationrules"].'</a> &raquo; <a href="index.php?_m=tickets&_a=editescalationrule&escalationruleid='. intval($_escalationrule["escalationruleid"]) .'" title="'.$_SWIFT["language"]["edit"].'">'.$_SWIFT["language"]["edit"].'</a>', $_SWIFT["language"]["desc_escalationrules"] , 7);
	
	// Check for sla plans
	if (!count($_SWIFT["slaplancache"]))
	{
		$errormessage = iif($errormessage=="", $_SWIFT["language"]["errorinsertrule"], $errormessage);
	}

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderEscalationForm(EDIT);

	$template->assign("backurl", "index.php?_m=tickets&_a=manageescalationrules");

	$interface->adminFooter();
}

?>